<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
      label-position="left">

      <div class="title-container">
        <h3 class="title">办公管理系统</h3>
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input ref="mobile" v-model="loginForm.mobile" placeholder="请输入账号" name="mobile" type="text" tabindex="1"
          auto-complete="on" />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
          placeholder="请输入密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin">登录</el-button>
      <div class="tips">
        <span style="margin-right:20px;">账号: 13800000000</span>
        <span> 密码: 任意</span>
      </div>

    </el-form>
  </div>
</template>

<script>
  import {
    validMobile
  } from '@/utils/validate'
  import {
     getUserProfileAPI
  } from '@/api'

  export default {
    name: 'Login',
    data() {
      // 自定义验证规则(手机号)
      const validMobiles = (rule, value, callback) => {
        if (!validMobile(value)) {
          callback(new Error('请输入正确的用户名'))
        } else {
          callback()
        }
      }
      return {
        // 表单对象
        loginForm: {
          mobile: '13800000002',
          password: '123456'
        },
        // 规则对象
        loginRules: {
          mobile: [{
            required: true,
            trigger: 'blur',
            validator: validMobiles
          }],
          password: [{
            required: true,
            trigger: 'blur',
            min: 6,
            max: 11,
            message: '密码需要6~11位'
          }]
        },
        loading: false, // 登录按钮图标状态标识
        passwordType: 'password', // 密码类型
        redirect: undefined // 存储登录成功后需要跳转的地址值
      }
    },
    watch: {
      // 检测路由变化
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      // 显示/隐藏 密码
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = 'text'
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      // 登录按钮的方法
      handleLogin() {
        this.$refs.loginForm.validate(async valid => {
          if (valid) {
            // 知识点1: await 只能提取Promise对象成功的状态的值
            // 知识点2: await 取代then函数,await可以把成功的值提取出来留在原地
            // 知识点3: 如何捕获Promise错误的状态呢?使用try catch代码块
            // 知识点4: actions方法,无论何时都返回Promise对象(自定义,非axios的)
            try{
              // 坑: 不加await,会调用登录接口的时候,这个登录异步无结果的时候,代码会继续往下执行
              // 即使密码错误了也会跳转,因为不加await,promise状态为pending(初始化的时候),
              // 加await等待后面成功了,才会继续往下走
               await this.$store.dispatch('user/loginActions',this.loginForm)
              // 简单封装 message 组件
              this.$message('登录成功')
              // this.$route.path > 路由地址 例如 '/info'
              // this.$route.fullpath > 路由地址和参数 例如: '/info?a=10&b=20'
              // 问题: 拿到刚才退出登录时,回传的未遂地址的路径字符串呢?
              // 答案: this.$route.query(vue-router内会把?后面参数字符串转成对象挂载query属性上)
              // || 前面有地址就跳转回未遂页面,如果前面没有(证明是第一次登录,就用后面的值跳转去首页)
              this.$router.replace(this.$route.query.redirect || '/')
            }catch(e){
              this.$message(e,'error')
            }
          }
        })
      }
    },
  }
</script>

<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg: #283443;
  $light_gray: rgb(157, 201, 255);
  $cursor: rgb(157, 201, 255);

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .login-container {
    background: url('../../assets/common/login.jpg');
  background-position: center;

    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgb(211, 229, 255);
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;

  .el-form-item__error {
    font-size: 14px;
  }

  .login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;

    .login-form {
      position: relative;
      width: 520px;
      max-width: 100%;
      padding: 160px 35px 0;
      margin: 0 auto;
      overflow: hidden;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
</style>
